<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper(array('cookie','form'));
		$this->load->model('user');
	}

	public function index()
	{
		
		$this->load->view('login');
	}

	public function login()
	{
		$u = array('email' =>$this->input->post('account'),
				'pwd' =>$this->input->post('password'),
				 );
		$user = $this->user->get_by_email($u['email']);

		if ($user) {
			if ($user->pwd == md5($u['pwd'].'twohome'.$user->name) ) {
				$s = array('name' => $user->name,
		 		'uid' => $user->use_rid,
		 		'login' => TRUE);
				 $this->session->set_userdata($s);

				 redirect('');
			}
		}
	}

	public function reg()
	{
		$u = array('name' => $this->input->post('id'),
				'email' =>$this->input->post('account'),
				'pwd' =>$this->input->post('password'),
				 );
		$u['pwd'] = md5($u['pwd'].'twohome'.$u['name']);
		$uid = $this->user->add($u);
		$s = array('name' => $u['name'],
		 		'uid' => $uid,
		 		'login' => TRUE);
		 $this->session->set_userdata($s);

		 redirect('');
	}
}

